/**
  * 登陆注册页
  */
module game {

    export class MainUIMediator extends BaseMediator {
        public static NAME: string = "MainUIMediator";
        public constructor(viewComponent: any = null) {
            super(MainUIMediator.NAME, viewComponent);
        }

        public listNotificationInterests(): Array<any> {
            return [
                PanelNotify.HALL.STAGE_RESIZE,
                PanelNotify.LOGIN.OPEN_MAIN,
                PanelNotify.LOGIN.CLOSE_MAIN
            ];
        }
        private panel: MainUIPanel;
        public handleNotification(notification: puremvc.INotification): void {
            switch (notification.getName()) {
                case PanelNotify.HALL.STAGE_RESIZE:
                    if (this.panel) {
                        this.panel.width = GameConfig.curWidth()
                        this.panel.height = GameConfig.curHeight()
                    }
                    break;
                case PanelNotify.LOGIN.OPEN_MAIN: {
                    this.panel = null;
                    this.panel = new MainUIPanel();
                    this.showUI(this.panel, 1, 1, true);
                    break;
                }
                case PanelNotify.LOGIN.CLOSE_MAIN: {
                    this.closePanel(0);
                    break;
                }
            }
        }

        /**
         * 初始化面板ui
         */
        public initUI(): void {
            this.panel.logo.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onTouristBtn, this);
            this.panel.btn_custome.addEventListener(egret.TouchEvent.TOUCH_TAP, this.loginTourist, this);
            this.panel.btn_customerservice.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onCustormServiceBtnClick, this);

        }

        /**
         * 初始化面板数据
         */
        public initData(): void {
            this.panel.bg.source = "login_bg"
        }
        //打开登陆页
        private onTouristBtn(event: egret.TouchEvent) {
            SoundManage.startAnniuBtnLoad();
            EffectUtils.playDim(event.currentTarget);
            game.AppFacade.instance.sendNotification(PanelNotify.LOGIN.OPEN_PLAYERLOGIN);
        }
        //试玩
        private loginTourist(event: egret.TouchEvent) {
            SoundManage.startAnniuBtnLoad();
            EffectUtils.playDim(event.currentTarget);
            EffectUtils.showWaritPanel();
            var request = new egret.HttpRequest();
            request.responseType = egret.HttpResponseType.TEXT;
            request.open(InstructionsEnum.IP_API + "/H5Interface/UserTryToLogin?AgentPlatForm=yuezhong"
                , egret.HttpMethod.POST);
            request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            request.send();
            request.addEventListener(egret.Event.COMPLETE, (event: egret.Event) => {
                try {
                    var request = <egret.HttpRequest>event.currentTarget;
                    var obj = JSON.parse(request.response)
                    if (obj.code == 0) {
                        EffectUtils.hideWaritPanel();
                        let mess = JSON.parse(obj.mess)
                        let objc = {
                            dwUserName: mess.username,
                            platform: mess.platform,
                            token: mess.token
                        }
                        InstructionsEnum.UserName = objc.dwUserName;
                        InstructionsEnum.platForm = objc.platform;
                        InstructionsEnum.token = objc.token;
                        InstructionsEnum.isGuest = "1";
                        SocketManager.sendMessage(InstructionsEnum.GAME_TYPE_NAME.HALL, RegDataGame.CMD_MB_LogonUserApi, objc, true);
                    }
                    else {
                        EffectUtils.hideWaritPanel();
                        Global.tipShow(obj.mess)
                    }
                } catch (err) {
                    EffectUtils.textFadeAway("数据包异常，无法解析!")
                }
            }, this);

        }
        //打开客服
        private onCustormServiceBtnClick(event: egret.TouchEvent): void {
            SoundManage.startAnniuBtnLoad();
            let text = "https://op.f88.live800.com/live800/chatClient/chatbox.jsp?companyID=1072027&configID=152538&jid=5882571089&s=1"
            EffectUtils.showWaritPanel();
            egret.setTimeout(() => {
                EffectUtils.hideWaritPanel();
            }, this, 1000);
            JumpPayUrl(text);
            // game.AppFacade.instance.sendNotification(PanelNotify.OPEN_CUSTOMER_SERVICE);
        }

    }
}
